﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;

public partial class Month_ModifyMonthHandShake : System.Web.UI.Page
{
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    MonthHandShakeManager dllMonthHandShakeManager = new MonthHandShakeManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpQuarterDate);
            bindDropDownList(this.drpMonthDate);
            bindGridView();
        }
    }


    /// <summary>
    /// 绑定所有的dropdownlist
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        //年份绑定
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            DataTable _year = dllQuarterDateManager.getQuarterDate();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        }

        //绑定年计划周期
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期

                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                }

                #endregion
                bindDropDownList(this.drpMonthDate);
            }
            else
            {
                ListItem _li = new ListItem("无年周期");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(_li);
                bindDropDownList(this.drpMonthDate);
            }

        }

        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                //根据系统当前日期查找到对应的年周期
                if (Request.QueryString["MonthDate"] != null)
                {
                    this.drpMonthDate.SelectedValue = Utility.HttpHelp.getParamsString("MonthDate");
                }
                else
                {
                    DataTable _monthtime = dllMonthDateManager.getMonthDate();
                    if (_monthtime.Rows.Count == 1)
                    {
                        this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                        this.drpMonthDate.DataSource = _monthtime;
                        this.drpMonthDate.DataTextField = "MonthName";
                        this.drpMonthDate.DataValueField = "Month_Id";
                        ListItem _li = this.drpMonthDate.SelectedItem;
                        if (_li != null)
                        {
                            if (_li.Text != "无月周期")
                            {
                                bindGridView();
                            }
                        }
                    }
                }
                #endregion

            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);
                this.gdvHandShake.DataSource = createTable();
                this.gdvHandShake.DataBind();
            }
        }
    }

    /// <summary>
    /// 生成空白行
    /// </summary>
    /// <returns></returns>
    DataTable createTable()
    {
        DataTable _dt = new DataTable();
        DataColumn dc = new DataColumn("HandShakeId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CreateDate");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MonthDate");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Sponsor");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Participant");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CompleteTime");
        _dt.Columns.Add(dc);
        dc = new DataColumn("HandShakeContents");
        _dt.Columns.Add(dc);
        dc = new DataColumn("IsConsensus");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CheckNote");
        _dt.Columns.Add(dc);
        dc = new DataColumn("IsDelete");
        _dt.Columns.Add(dc);
        dc = new DataColumn("HandShakeState");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MeetingContent");
        _dt.Columns.Add(dc);
        DataRow dr = _dt.NewRow();
        dr["HandShakeId"] = "";
        dr["CreateDate"] = "";
        dr["MonthDate"] = "";
        dr["Sponsor"] = "";
        dr["Participant"] = "";
        dr["CompleteTime"] = "";
        dr["HandShakeContents"] = "";
        dr["IsConsensus"] = "";
        dr["CheckNote"] = "0";
        dr["IsDelete"] = "";
        dr["HandShakeState"] = "";
        dr["MeetingContent"] = "";
        _dt.Rows.Add(dr);
        return _dt;
    }

    /// <summary>
    /// 绑定握手关系表格
    /// </summary>
    void bindGridView()
    {
        object[] _value = new object[1];
        _value[0] = Utility.HttpHelp.getParamsInt("HandShakeId");
        DataTable _monthHandShake = dllMonthHandShakeManager.get(MonthHandShakeManager.field.HandShakeId.ToString(), _value);
        this.gdvHandShake.DataSource = _monthHandShake;
        this.gdvHandShake.DataBind();
        for (int i = 0; i < this.gdvHandShake.Rows.Count; i++)
        {
            string _Lengthstart = this.gdvHandShake.DataKeys[i]["CompleteTime"].ToString();
            if (_Lengthstart.Length > 9)
            {
                _Lengthstart = _Lengthstart.Substring(0, 9);//截取字符串，从左边截取10个字符串
                TextBox _completeTime = (TextBox)this.gdvHandShake.Rows[i].Cells[4].FindControl("txtCompleteTime");
                _completeTime.Text = _Lengthstart;
            }

            if (Convert.ToInt32(this.gdvHandShake.DataKeys[i]["IsConsensus"].ToString()) == 1)
            {
                //this.gdvHandShake.Rows[i].Cells[6].Text = "达成共识";
                DropDownList drpIsConsensus = (DropDownList)this.gdvHandShake.Rows[i].Cells[6].FindControl("drpIsConsensus");
                drpIsConsensus.SelectedIndex = drpIsConsensus.Items.IndexOf(drpIsConsensus.Items.FindByValue(_monthHandShake.Rows[i]["IsConsensus"].ToString()));
            }
            else
            {
                DropDownList drpIsConsensus = (DropDownList)this.gdvHandShake.Rows[i].Cells[6].FindControl("drpIsConsensus");
                drpIsConsensus.SelectedIndex = drpIsConsensus.Items.IndexOf(drpIsConsensus.Items.FindByValue(_monthHandShake.Rows[i]["IsConsensus"].ToString()));
            }
            DropDownList drp = (DropDownList)this.gdvHandShake.Rows[i].Cells[7].FindControl("drpType");
            drp.SelectedIndex = drp.Items.IndexOf(drp.Items.FindByValue(_monthHandShake.Rows[i]["HandShakeState"].ToString()));

            TextBox txtCheckNote = (TextBox)this.gdvHandShake.Rows[i].Cells[7].FindControl("txtCheckNote");
            txtCheckNote.Text = this.gdvHandShake.DataKeys[i]["CheckNote"].ToString();
            TextBox tb = (TextBox)this.gdvHandShake.Rows[i].FindControl("txtHandShakeContents");
            tb.Text = this.gdvHandShake.DataKeys[i]["HandShakeContents"].ToString();

        }

    }


    /// <summary>
    /// 自动编号
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvHandShake_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }
    }


    /// <summary>
    /// 修改事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        int _monthHandShakeId = Utility.HttpHelp.getParamsInt("HandShakeId");
        for (int i = 0; i < this.gdvHandShake.Rows.Count; i++)
        {
            object[] _value = new object[7];
            TextBox tb = (TextBox)this.gdvHandShake.Rows[i].Cells[3].FindControl("txtCompleteTime");
            //完成时间
            _value[0] = Convert.ToDateTime(tb.Text);
            //握手关系内容
            tb = (TextBox)this.gdvHandShake.Rows[i].Cells[5].FindControl("txtHandShakeContents");
            _value[1] = tb.Text;
            //握手共识状态
            DropDownList drpIsConsensus = (DropDownList)this.gdvHandShake.Rows[i].Cells[6].FindControl("drpIsConsensus");
            _value[2] = Convert.ToInt32(drpIsConsensus.SelectedValue);
            //握手完成状态
            DropDownList drp = (DropDownList)this.gdvHandShake.Rows[i].Cells[6].FindControl("drpType");
            _value[3] = Convert.ToInt32(drp.SelectedValue);
            tb = (TextBox)this.gdvHandShake.Rows[i].Cells[6].FindControl("txtCheckNote");
            //评估意见
            _value[4] = tb.Text;
            _value[5] = _monthHandShakeId;
            _value[6] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
            int result = dllMonthHandShakeManager.editMonthHandShakeContent(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("修改月握手未成功，请重试！");
            }
            else
            {
                int MonthDate = Utility.HttpHelp.getParamsInt("MonthDate");
                int Year = Utility.HttpHelp.getParamsInt("Year");
                int QuarterDate = Utility.HttpHelp.getParamsInt("QuarterDate");
                Utility.HttpHelp.closeWindow("MonthHandShake.aspx?MonthDate=" + MonthDate + "&Year=" + Year + "&QuarterDate=" + QuarterDate, this.Page);
                //Page.RegisterStartupScript("refresh", "<script>window.opener.location.href=window.opener.location.href;self.close();</script>");
            }

        }

    }
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
    }
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);

    }
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _li = this.drpMonthDate.SelectedItem;
        if (_li != null)
        {
            if (_li.Text != "无月周期")
            {
                bindGridView();
            }
            else
            {
                this.gdvHandShake.DataSource = createTable();
                this.gdvHandShake.DataBind();
            }
        }
    }
}
